/****************************************************
          File Name:epoll.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 03 Mar 2015 10:29:49 AM CST
*****************************************************/
/*
 * 1, create epoll instance (epoll_create)
 * 2, add to events (epoll_ctl)
 * 3, polling   (epoll_wait)
 */

#include<stdio.h>
#include<sys/epoll.h>
#include<stdlib.h>
#include<unistd.h>

static void err(const char *s)
{
    perror(s);
    exit(1);
}

int main(int argc, char **argv)
{
    int efd, res; 
    char BUF[10];
    struct epoll_event ev;

    ev.events = EPOLLIN;
    ev.data.fd = STDIN_FILENO;

    efd = epoll_create(1); // 1 will bre ignored
    if(efd == -1)
        err("epoll_create");
    if(epoll_ctl(efd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
        err("epoll_ctl");
   for(;;)
   { // 1 ->maxevents, i.e. the length of the 'struct epoll_events' array pointed to by '*events'
        res = epoll_wait(efd, &ev, 1, 5000);
        if(res == -1)
        err("epoll_wait");
        if(res == 0)
            printf("---timeout----\n");
        else
        {
            read(STDIN_FILENO, BUF, 10);
            printf("**%s**\n", BUF);
            break;
        }
   }
    close(efd);
}
